export function getImageUrl(name: string) {
  return new URL(`../assets/images/${name}`, import.meta.url).href;
}

export function getCoverImageUrl(name: string) {
  return new URL(`../assets/images/coverImages/${name}.png`, import.meta.url)
    .href;
}

export function getSvgCompUrl(name: string) {
  return new URL(`../assets/images/svgComp/${name}`, import.meta.url).href;
}

export const debounce = (func, delay) => {
  let timer = null;
  return function () {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      func.apply(this, arguments);
    }, delay);
  };
};

export const GenNonDuplicateID = (randomLength) => {
  return Number(
    Math.random().toString().substr(3, randomLength) + Date.now()
  ).toString(36);
};

export function throttle(fn, interval) {
  let lastTime = 0;
  const _throttle = function () {
    const nowTime = new Date().getTime();
    const remainTime = interval - (nowTime - lastTime);
    if (remainTime <= 0) {
      fn();
      lastTime = nowTime;
    }
  };
  return _throttle;
}

export const stringify = (obj) => {
  try {
    return JSON.stringify(obj, (k, v) => {
      if (typeof v === "function") {
        return `FUNCTION_FLAG ${v}`;
      } else {
        return v;
      }
    });
  } catch (error) {
    console.log(error);
    return "出错了";
  }
};

export const parse = (jsonStr) => {
  try {
    return JSON.parse(jsonStr, (key, value) => {
      if (value && typeof value === "string") {
        return value.indexOf("FUNCTION_FLAG") > -1
          ? new Function(`return ${value.replace("FUNCTION_FLAG", "")}`)()
          : value;
      }
      return value;
    });
  } catch (error) {
    console.log(error);
    return "出错了";
  }
};

export function arraysAreEqual(arr1: string[], arr2: string[]): boolean {
  if (arr1.length !== arr2.length) {
    return false;
  }

  for (let i = 0; i < arr1.length; i++) {
    if (arr1[i] !== arr2[i]) {
      return false;
    }
  }

  return true;
}

// 写一个深拷贝函数
export function deepClone(obj) {
  if (typeof obj !== "object" || obj === null) {
    return obj;
  }

  if (Array.isArray(obj)) {
    const arr = [];
    for (let i = 0; i < obj.length; i++) {
      arr.push(deepClone(obj[i]));
    }
    return arr;
  }

  const newObj = {}; // 创建一个空对象
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      newObj[key] = deepClone(obj[key]); // 递归调用深拷贝函数
    }
  }

  return newObj;
}

export function isEqual(obj1, obj2) {
  // 检查两个对象的原始值是否相同
  if (obj1 === obj2) return true;
  // 检查两个对象是否都是对象
  if (
    typeof obj1 !== "object" ||
    obj1 === null ||
    typeof obj2 !== "object" ||
    obj2 === null
  ) {
    return false;
  }
  // 检查两个对象的键的数量是否相同
  if (Object.keys(obj1).length !== Object.keys(obj2).length) return false;
  // 遍历对象的所有键
  for (let key in obj1) {
    // 检查obj2是否拥有相同的键
    if (obj2[key] === undefined) return false;
    // 递归比较键对应的值
    if (!isEqual(obj1[key], obj2[key])) return false;
  }
  // 所有值都相同，返回true
  return true;
}

const { ipcRenderer } = window;

export async function getRequestUrl(key) {
  try {
    let path = ipcRenderer.sendSync("getTemplatePath", "config.json");
    let file = await ipcRenderer.invoke("readFile", path);
    // console.log("config.json", JSON.parse(file));
    return JSON.parse(file)[key];
  } catch (error) {
    ElMessage.error(error);
  }
}

//  查找数组中的某一个属性最大的值

export const findMaxValue = (arr, key) => {
  if (!arr || arr.length === 0) return null;
  return arr.reduce((max, item) => {
    return item[key] > max ? item[key] : max;
  }, arr[0][key]);
};

//  查找数组中的某一个属性最小的值

export const findMinValue = (arr, key) => {
  if (!arr || arr.length === 0) return null;
  return arr.reduce((min, item) => {
    return item[key] < min ? item[key] : min;
  }, arr[0][key]);
};
